//
//  ICReadVoiceView.m
//  speakVoiceDemo
//
//  Created by kingboyrang on 2021/5/1.
//  Copyright © 2021 wulanzhou. All rights reserved.
//

#import "ICReadVoiceView.h"
#import "ICRecordManager.h"
#import "VoiceConverter.h"

#define marginLeftRight 19.0f
#define marginTop       10.0f

@interface ICReadVoiceView ()<ICRecordManagerDelegate>
//背景
@property (nonatomic, strong) UIImageView *bubbleView;
//时长
@property (nonatomic, strong) UILabel     *durationLabel;
//语音动画
@property (nonatomic, strong) UIImageView *voiceIcon;
//语音按钮
@property (nonatomic, strong) UIButton    *voiceButton;
@property (nonatomic, strong) NSString    *voicePath;
@property (nonatomic, strong) UIImageView *currentVoiceIcon;

@end

@implementation ICReadVoiceView


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {

        [self addSubview:self.bubbleView];
        [self addSubview:self.durationLabel];
        [self addSubview:self.voiceButton];
        [self addSubview:self.voiceIcon];
        //默认显示方向
        self.showDirection = VoiceShowDirectionRight;
    }
    return self;
}

- (void)setShowDirection:(VoiceShowDirection)showDirection
{
    if (_showDirection!=showDirection) {
        _showDirection = showDirection;
        
        if (showDirection == VoiceShowDirectionLeft) {
            
            //背景
           CGRect r = self.bubbleView.frame;
           r.origin.x = marginLeftRight;
           self.bubbleView.frame = r;
           self.bubbleView.image = [UIImage imageNamed:@"liaotianbeijing1"];
           //时长
           r = self.durationLabel.frame;
           r.origin.x = CGRectGetMaxX(self.bubbleView.frame) + 20;
           self.durationLabel.frame = r;
           //语音动画
           r = self.voiceIcon.frame;
           r.origin.x = 30;
           self.voiceIcon.frame = r;
            
            self.voiceIcon.image = [UIImage imageNamed:@"left-3"];
            UIImage *image1 = [UIImage imageNamed:@"left-1"];
            UIImage *image2 = [UIImage imageNamed:@"left-2"];
            UIImage *image3 = [UIImage imageNamed:@"left-3"];
            self.voiceIcon.animationImages = @[image1, image2, image3];
           
           self.voiceIcon.animationDuration = 0.8;
           //语音按钮
           r = self.voiceButton.frame;
           r.origin.x = self.bubbleView.frame.origin.x;
           self.voiceButton.frame = r;
            
        }else if(showDirection == VoiceShowDirectionRight){
            
            //背景
            CGRect r = self.bubbleView.frame;
            r.origin.x = self.frame.size.width - 100 - marginLeftRight;
            self.bubbleView.frame = r;
            self.bubbleView.image = [UIImage imageNamed:@"liaotianbeijing2"];
            //时长
            r = self.durationLabel.frame;
            r.origin.x = self.frame.size.width - (self.bubbleView.frame.size.width + marginLeftRight + 30);
            self.durationLabel.frame = r;
            //语音动画
            r = self.voiceIcon.frame;
            r.origin.x = self.frame.size.width - r.size.width -30;
            self.voiceIcon.frame = r;
            
            self.voiceIcon.image = [UIImage imageNamed:@"right-3"];
            UIImage *image1 = [UIImage imageNamed:@"right-1"];
            UIImage *image2 = [UIImage imageNamed:@"right-2"];
            UIImage *image3 = [UIImage imageNamed:@"right-3"];
            self.voiceIcon.animationImages = @[image1, image2, image3];
            self.voiceIcon.animationDuration = 0.8;
            //语音按钮
            r = self.voiceButton.frame;
            r.origin.x = self.bubbleView.frame.origin.x;
            self.voiceButton.frame = r;
            
        }
    }
}

//属性重写
- (void)setVoiceReadPath:(NSString *)voiceReadPath{
    if (_voiceReadPath!=voiceReadPath) {
        _voiceReadPath = voiceReadPath;
        
        if (self.showDirection == VoiceShowDirectionRight) {
            self.durationLabel.text  = [NSString stringWithFormat:@"%ld''",[[ICRecordManager shareManager] durationWithVideo:[NSURL fileURLWithPath:voiceReadPath]]];
        }else{
             self.durationLabel.text  = [NSString stringWithFormat:@"''%ld",[[ICRecordManager shareManager] durationWithVideo:[NSURL fileURLWithPath:voiceReadPath]]];
        }
    }
}

#pragma mark - 属性
- (UIImageView *)bubbleView {
    //256
    if (_bubbleView == nil) {
        _bubbleView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width - 100 - marginLeftRight, marginTop, 100, 40)];
        //_bubbleView.image    = [UIImage imageNamed:@"liaotianbeijing2"];
    }
    return _bubbleView;
}
- (UIButton *)voiceButton{
    if (_voiceButton ==nil) {
        _voiceButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _voiceButton.frame = CGRectMake(self.bubbleView.frame.origin.x, marginTop, 100, 40);
         [_voiceButton addTarget:self action:@selector(voiceButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _voiceButton;
}
- (UILabel *)durationLabel{
    if (_durationLabel == nil) {
        CGFloat left=self.frame.size.width - (self.bubbleView.frame.size.width + marginLeftRight + 30);
        _durationLabel = [[UILabel alloc] initWithFrame:CGRectMake(left, (self.frame.size.height - 20)/2.0, 50, 20)];
        _durationLabel.font = [UIFont systemFontOfSize:16.0];
    }
    return _durationLabel;
}
- (UIImageView *)voiceIcon{
    if (_voiceIcon==nil) {
        _voiceIcon = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width - 11 -30, (self.frame.size.height - 16.5)/2.0, 11, 16.5)];
    }
    return _voiceIcon;
}
//播放
- (void)voiceButtonClicked:(UIButton *)voiceBtn
{
    voiceBtn.selected = !voiceBtn.selected;
    [self playVoice];
     
}
// 文件路径
- (NSString *)mediaPath:(NSString *)originPath
{
    // 这里文件路径重新给，根据文件名字来拼接
    NSString *name = [[originPath lastPathComponent] stringByDeletingPathExtension];
    return [[ICRecordManager shareManager] receiveVoicePathWithFileKey:name];
}
// play voice
- (void)playVoice
{
    ICRecordManager *recordManager = [ICRecordManager shareManager];
    recordManager.playDelegate = self;
    // 文件路径
    NSString *voicePath = [self mediaPath:self.voiceReadPath];
    NSString *amrPath   = [[voicePath stringByDeletingPathExtension] stringByAppendingPathExtension:@"amr"];
    [VoiceConverter ConvertAmrToWav:amrPath wavSavePath:voicePath];
   
    if (self.voicePath) {
        if ([self.voicePath isEqualToString:voicePath]) { // the same recoder
            self.voicePath = nil;
            [[ICRecordManager shareManager] stopPlayRecorder:voicePath];
            [self.voiceIcon stopAnimating];
            self.currentVoiceIcon = nil;
            return;
        } else {
            [self.currentVoiceIcon stopAnimating];
            self.currentVoiceIcon = nil;
        }
    }
    [[ICRecordManager shareManager] startPlayRecorder:voicePath];
    [self.voiceIcon startAnimating];
    self.voicePath = voicePath;
    self.currentVoiceIcon = self.voiceIcon;
}

#pragma mark - ICRecordManagerDelegate
- (void)voiceDidPlayFinished
{
    self.voicePath = nil;
    ICRecordManager *manager = [ICRecordManager shareManager];
    manager.playDelegate = nil;
    [self.currentVoiceIcon stopAnimating];
    self.currentVoiceIcon = nil;
}


@end
